//
//  SettingView.m
//  mid
//
//  Created by itlab on 2020/12/4.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SettingView.h"
#import "../utils/utility.h"

@implementation SettingView

-(void)viewInit{
    [self setBackgroundColor:[UIColor whiteColor]];
    [self switchInit];
    [self settingTableInit];
    [self msgBtnInit];
    [self loginBtnInit];
}
-(void)loginBtnInit{
    if (_loginButton == nil) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    }
    _loginButton.frame = CGRectMake(0, 0, 160, 160);
    _loginButton.layer.cornerRadius = 80;
    _loginButton.layer.borderWidth = 1.0;
    _loginButton.clipsToBounds = YES;
    _loginButton.backgroundColor = [UIColor whiteColor];
    _loginButton.center = CGPointMake(self.center.x,200);
    
    _loginButton.titleLabel.textColor = [UIColor blackColor];
    _loginButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [_loginButton setTitle:@"登录" forState:(UIControlState) normal];
    [_loginButton setTitleColor:[UIColor blackColor] forState:(UIControlState) normal];
    [self addSubview:_loginButton];
}
-(void)msgBtnInit{
    if(_message_button == nil){
        _message_button = [UIButton buttonWithType:UIButtonTypeSystem];
        [_message_button setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-100, [UIScreen mainScreen].bounds.size.height-170, 60, 60)];
        [_message_button setBackgroundColor:[UIColor whiteColor]];
        
        CGSize image_size = CGSizeMake(60, 50);
        UIImage *msg_icon_origin = [UIImage imageNamed:@"icon_msg.png"];
        UIImage *msg_icon = scaleToSize(msg_icon_origin, image_size);
        UIImage *msg_icon_render = [msg_icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [_message_button setImage: msg_icon_render forState:UIControlStateNormal];
        
        _message_button.layer.cornerRadius = _message_button.frame.size.width/2;
        _message_button.clipsToBounds = YES;
        _message_button.layer.borderColor = [UIColor darkGrayColor].CGColor;
        _message_button.layer.borderWidth = 1.0f;
        
        UIPanGestureRecognizer *pan_gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [_message_button addGestureRecognizer:pan_gesture];
    }
    [self addSubview:_message_button];
}
-(void)switchInit{
    if (_style_switch == nil) {
        _style_switch = [[UISwitch alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-60, 15, 50, 30)];
    }
}
-(void)settingTableInit{
    if(_setting_table == nil){
        double status_bar_height = [[UIApplication sharedApplication] statusBarFrame].size.height;
        CGRect frame = CGRectMake(0, status_bar_height+305, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-270);
        _setting_table = [[UITableView alloc] initWithFrame: frame style:UITableViewStyleGrouped];
    }
    [self addSubview:_setting_table];
}
-(void)showAvatar:(UIImage*)avatar{
    [_loginButton removeFromSuperview];
    
    _avatar = [[UIImageView alloc]init];
    _avatar.frame = CGRectMake(0, 0, 160, 160);
    _avatar.layer.cornerRadius = 80;
    _avatar.layer.masksToBounds = YES;
    _avatar.layer.borderWidth = 1.0;
    _avatar.backgroundColor = [UIColor yellowColor];
    _avatar.center = CGPointMake(self.center.x, 200);
    
    // TODO 好像API没有修改头像的,没头像太丑了，临时加个
    NSURL *url = [[NSURL alloc]initWithString:@"https://hbimg.huabanimg.com/f3085171af2a2993a446fe9c2339f6b2b89bc45f4e79d-LacPMl_fw658"];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    avatar = [[UIImage alloc]initWithData:data];
    
    _avatar.image = avatar;
    [self addSubview:_avatar];
}
-(void)panAction:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation_point = [recognizer translationInView:self];
    CGPoint center = recognizer.view.center;
    recognizer.view.center = CGPointMake(center.x+translation_point.x, center.y+translation_point.y);
    [recognizer setTranslation:CGPointZero inView:self];
}

@end
